package com.maxima.maximaAttendance.actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.maxima.maximaAttendance.daos.TimeLogDAO;
import com.maxima.maximaAttendance.daos.UserDAO;
import com.maxima.maximaAttendance.pojos.TimeLog;
import com.maxima.maximaAttendance.pojos.User;
import com.opensymphony.xwork2.ActionSupport;

public class UserList extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 1L;

	private Map<String,Object> map;
	private List<User> usersList;
	//private Map<Integer,TimeLog> timeLogs;
	
	@Override
	public String execute() throws Exception {
		if(map.get("firstLogin")==null)
			return ERROR;
		this.setUsersList(initUserList());
		map.put("usersList", usersList);
		return SUCCESS;
	}
	public String welcome()
	{
		this.setUsersList(initUserList());
		map.put("usersList", usersList);
		return "welcome";	
	}

	public List<User> initUserList() {
		
		TimeLogDAO timeLogDAO=new TimeLogDAO();
		SimpleDateFormat dateFormat= new SimpleDateFormat("dd-MM-yyyy");
		Date date= new Date();
		List<User> lst=new UserDAO().getAllUsers();
		
//		for (User u : lst) {
//			TimeLog t=timeLogDAO.getLastTimeLog(u);
//			if(u.getLogged_in()==true)
//			{
////				u.setTime_in(t.getTimeIn());
//			}
//			else if(u.getLogged_in()==false)
//			{
//				t=timeLogDAO.getTimeLogByDate(u, dateFormat.format(date));
//				if(t==null)
//				{
//					u.setPresent(false);
//				}
//				else if(t.getWorkFromHome()==true)
//				{
//					u.setPresent(false);
//					u.setWorkFromHome(true);
//				}
//				else
//				{
//					u.setPresent(true);
////					u.setTime_in(t.getTimeIn());
////					u.setTime_out(t.getTimeOut());
//				}
//			}
//		}
		return lst;
	}

	@Override
	public void setSession(Map<String, Object> m) {
		this.map=m;
	}

	public List<User> getUsersList() {
		return usersList;
	}

	public void setUsersList(List<User> usersList) {
		this.usersList = usersList;
	}

}
